<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>仓库管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../../../resources/lib/layui-v2.5.5/css/layui.css" media="all" />
    <link rel="stylesheet" href="../../../../resources/css/public.css" media="all" />
</head>
<body >
<div class="layuimini-container">
<!--搜索条件开始-->
	 <div class="layuimini-main">
		<fieldset class="layui-elem-field layuimini-search">
			<legend>搜索信息</legend>
			<div style="margin: 10px 10px 10px 10px">
				<form class="layui-form layui-form-pane" lay-filter="searchFrm" id="searchFrm" name="searchFrm" method="post">
					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">公告标题</label>
							<div class="layui-input-inline">
								<input type="text" name="title" id="title" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">发布员</label>
							<div class="layui-input-inline">
								<input type="text" name="opername" id="opername" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">开始时间</label>
							<div class="layui-input-inline">
								<input type="text" name="startTime" id="startTime" readonly="readonly" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">结束时间</label>
							<div class="layui-input-inline">
								<input type="text" name="endTime" id="endTime" readonly="readonly" autocomplete="off" class="layui-input">
							</div>
						</div>

					</div>
					<div class="layui-form-item" style="text-align: center">
						<div class="layui-inline">
							<button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" lay-filter="doSearch" lay-submit="">查询</button>
							<button type="reset" class="layui-btn layui-icon layui-icon-refresh">重置</button>
						</div>
					</div>
		</form>
			</div>
		</fieldset>
		<!--搜索条件结束-->
	
<!--搜索条件结束-->

<!--数据表格开始-->
	
		<table class="layui-hide" id="NoticeTable" lay-filter="NoticeTable"></table>
		<div id="NoticeToolBar" style="display: none;">
			<button type="button" lay-event="addNotice" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-add-circle"></span>添加</button>
			<button type="button" lay-event="batchDelete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>批量删除</button>
		</div>
		<div id="NoticeRowBar" style="display: none">
			<button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
			<button type="button" lay-event="update" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-set-sm"></span>更新</button>
			<button type="button" lay-event="show" class="layui-btn layui-btn-sm layui-btn-warm"><span class="layui-icon layui-icon-play"></span>查看</button>
		</div>
	</div>
	
</div>
<!--数据表格结束-->

<!--添加和修改的弹出层开始-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv" >
    <form class="layui-form layui-form-pane"  lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">公告标题:</label>
            <div class="layui-input-block">
				<!--隐藏一个ID-->
                <input type="hidden" name="id">
                <input type="text" name="title"  placeholder="请输入公告标题" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公告内容:</label>
            <div class="layui-input-block"><!-- 这里新建一个编译器 -->
                <textarea class="layui-textarea layui-hide" name="content" lay-verify="content" id="content"></textarea>
            </div>
        </div>

        <div class="layui-form-item" style="text-align: center;">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release" lay-filter="doSubmit" lay-submit="">提交</button>
                <button type="reset" id="dataFrmResetBtn" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" >重置</button>
            </div>
        </div>
    </form>
</div>
<!--添加和修改的弹出层结束-->

<script src="../../../../resources/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../../resources/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../../resources/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../../resources/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../../../resources/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../../resources/common/common.js" charset="utf-8"></script>

<script>
    layui.use(['jquery','layer','form','laydate','table','layedit'],function () {
        var form=layui.form;
        var $=layui.jquery;
        var layer=layui.layer;
        var laydate=layui.laydate;
        var table=layui.table;
        var layedit = layui.layedit;

        //渲染时间选择器
        laydate.render({
            elem:'#startTime',
            type:'datetime'
        })

        laydate.render({
            elem:'#endTime',
            type:'datetime'
        })
        //初始化富文本编辑器
        var editIndex;

        var tableIndex = table.render({
            elem: '#NoticeTable'
            ,url: api+'notice/loadAllNotice'
            ,toolbar: '#NoticeToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '系统公告表'
            ,page: true
            ,cols: [ [
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID',align:'center',width:80}
                ,{field:'title', title:'标题',align:'center',width:300}
                ,{field:'opername', title:'发布人',align:'center',width:300}
                ,{field:'createtime', title:'发布时间',align:'center',width:300}
                ,{fixed: 'right', title:'操作', toolbar: '#NoticeRowBar',align:'center'}
            ] ]
            ,done:function(data,curr,count){
                //不是第一页如果返回的数据为0那么就返回上一页
                if(data.data.length==0&&curr!=1){
                    tableIndex.reload({
                        page:{
                            curr:curr-1
                        }
                    });
                }
            }
        });

        //模糊查询
        form.on('submit(doSearch)',function(data){
            var params = $("#searchFrm").serialize();
            tableIndex.reload({
                url: api+"notice/loadAllNotice?" + params,
                page:{
                    curr:1
                }
            })
            return false;
        });

        //头工具栏事件
        table.on('toolbar(NoticeTable)', function(obj){
            switch(obj.event){
                case 'batchDelete':
                    BatchDelete();
                    break;
                case 'addNotice':
                    openAddNotice();
                    break;
            };
        });


        //监听行工具条的事件
        table.on("tool(NoticeTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'delete':
                    DeleteNotice(data);
                    break;
                case 'update':
                    UpdateNotice(data);
                    break;
                case 'show':
                    ShowNotice(data);
                    break;
            };
        });

        var url;
        var mainIndex;

        //添加添加页面
        function openAddNotice() {
            mainIndex = layer.open({
                type:1,
                title:'添加公告',
                content:$("#saveOrUpdateDiv"),
                area:['800px','550px'],
                success:function(index){
                    editIndex=layedit.build('content'); //建立编辑器
                    //清空表单数据
                    layedit.setContent(editIndex,""); //清空富编译器里面的数据 一般的reset清空不了
                    $("#dataFrm")[0].reset();
                    url= api+"notice/addNotice";
                }
            });
        }
        //重置数据
        $("#dataFrmResetBtn").click(function(){
            //清空富编译器里面的数据 一般的reset清空不了
            layedit.setContent(editIndex,"");
        });

        //更新页面
       function UpdateNotice(data){
           mainIndex = layer.open({
               type:1,
               title:'添加公告',
               content:$("#saveOrUpdateDiv"),
               area:['800px','550px'],
               success:function(index){
                   editIndex=layedit.build('content'); //建立编辑器
                   //设置富文本里的数据
                   layedit.setContent(editIndex,data.content);//设置编译器里面的内容
                   form.val("dataFrm",data);
                   url= api+"notice/updateNotice";
               }
           });
        }

        //保存
        form.on("submit(doSubmit)",function(obj){
            layedit.sync(editIndex);  //把富文本里面的数据同步到自己写的textarea里面 这里一定要写到前面
            //序列化表单数据
            var params=$("#dataFrm").serialize();
            $.post(url,params,function(obj){
                layer.msg(obj.msg);
                //关闭弹出层
                layer.close(mainIndex);
                //刷新数据 表格
                if (obj.code ==200){
                    tableIndex.reload();
                }
            })
        });

        //批量删除
        function BatchDelete() {
            //得到选中行
            var checkStatus = table.checkStatus('NoticeTable');
            var dataLength = checkStatus.data.length;
            console.log(dataLength);
            if (dataLength>0){
                layer.confirm('你确定要删除这些数据吗?', {icon: 3, title:'提示'}, function(index){
                    var data = checkStatus.data; //获取选中行的数据
                    var ids = '';
                    $.each(data,function (index,item) {
                        if (index == 0){
                            ids += 'ids='+ item.id;
                        }else{
                            ids += '&ids='+ item.id;
                        }
                    })
                    $.post( api+"notice/batchDeleteNotice",ids,function (res) {
                        if(res.code==200){
                            tableIndex.reload();
                        }
                        layer.msg(res.msg);
                    })
                    layer.close(index);
                })
            }else{
                layer.msg("请选中行");
            }
        }

        //删除
        function  DeleteNotice(data) {
            layer.confirm('你确定要删除这条数据吗?', {icon: 3, title:'提示'}, function(index){
                $.post( api+"notice/deleteNotice",{id:data.id},function(res){
                    if(res.code==200){
                        tableIndex.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }

        //查看公告
		function ShowNotice(data){
			var title=data.title;
			var content=data.content;
			var noticeTime=data.createtime;
			var opername=data.opername;
			var html = '<div style="padding:15px 20px; text-align:justify; line-height: 22px;border-bottom:1px solid #e2e2e2;background-color: #2f4056;color: #ffffff">\n' +
			    '<div style="text-align: center;margin-bottom: 20px;font-weight: bold;border-bottom:1px solid #718fb5;padding-bottom: 5px"><h4 class="text-danger">' + title + '</h4></div>\n' +
			    '<div style="font-size: 12px">' + content + '</div>\n' +
			    '</div>\n';
			parent.layer.open({
			    type: 1,
			    title: '系统公告'+'<span style="float: right;right: 1px;font-size: 12px;color: #b1b3b9;margin-top: 1px">发布时间:'+noticeTime+'      发布人:'+opername+'</span>',
			    area: '800px;',
			    shade: 0.8,
			    btn: ['关闭'],
			    btnAlign: 'c',
			    moveType: 1,
			    content:html
			});
		}

    })
</script>
</body>
</html>